define(['jquery','api','user','qrcode'],function($,api,user){
	var vm,
	   controller = {
	   init:function(id){
	   	   if(!user.checkLogin())return;
		   api.setTitle('我要分享');
		   api.footerShow(0);
		   if(!vm){
		     this.render();
		   }else{
		     vm.show=true;
		   }
		   
	   },
	   render:function(){
	   	   var that=this;
	   	   $('#pages').append('<div id="share"></div>');
	       require(['text!pages/user/share.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#share',
	               data:{
	               	  show:true,
	               	  imgSrc:false,
	               	  dialogShow:false
	               },
	               methods:{
	                  showTop:function(){
	                     this.dialogShow=true;
	                  },
	                  hideTop:function(){
	                     this.dialogShow=false;
	                  }
	               },
	               template:tpl
	            });
	            if(!user.data.uid){
		            that.loadUserInfo();
		        }else{
		            that.createQr(user.data.uid);
		        }
	       })
	   },
	   createQr:function(uid){
	      var str='http://'+location.host+'#/agent/'+uid,
	          ct=$('#shareQrCt');
	      ct.qrcode({
	           render:'image',
	           background:"#ffffff",//背景颜色  
               foreground:"#000000", //前景颜色,
	           size:200,
	           text:str
	      });
	      setTimeout(function(){
	           vm.imgSrc=ct.find('img').attr('src');
	      },0);
	   	},
	   	loadUserInfo:function(){
	   		var that=this;
	   	    api.userInfo(function(rs){
		   	   if(rs.retcode=='0'){
		   	   	  user.data.uid=rs.userfund.uid;
		   	   	  user.data.username=rs.userfund.username;
		   	   	  that.createQr(user.data.uid);
		   	   }
		   });
	   	},
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	       api.footerShow(1);
	   }
	};
    return controller;
})